package models;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.BatchSize;

import models.core.SQLOrder;
import models.core.SQLParam;
import models.core.SQLUtils;
import play.data.validation.MaxSize;
import play.db.jpa.Model;

/**
 * 文章管理
 * 
 * @author zhangpeng
 * 
 */
@Entity
@Table(name = "article")
public class Article extends Model {

	/**
	 * 文章标题
	 */
	public String articleTitle;

	/**
	 * 文章主题
	 */
	public String articleSubject;

	/**
	 * 文章分类
	 */
	@ManyToOne
	public Category articleCategory;

	/**
	 * 文章内容
	 */
	@Column(columnDefinition="LongText")
	@MaxSize(10000)
	public String articleContent;

	/**
	 * 图片集
	 */
	
	public String articleContentImage;

	/**
	 * 文章作者
	 */
	public String articleAuthor;

	/**
	 * 审核状态 默认:不通过
	 */
	public Boolean articleAuditstatus;

	/**
	 * 推荐
	 */
	public Boolean articleCommendatory;

	/**
	 * 隐藏
	 */
	public Boolean articleHidden;

	/**
	 * 点击
	 */
	public Long articleCount;

	/**
	 * 创建时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Date createDate = new Date();

	/**
	 * 更新时间
	 */
	@Temporal(TemporalType.TIMESTAMP)
	public Date updateDate = new Date();
	
	
	@OneToMany(mappedBy="article")
	public List<Comment> comment;


	public String toString(){
		return articleTitle;
	}
	
	
	
	public static List<play.db.Model> hotArticle(int size){
		return findArticle(1, size, null, null, "createDate", "DESC", "articleAuditstatus=1 and articleCommendatory=1 and articleHidden=0");
	}
	
	public static List<play.db.Model> recentArticle(int size){
		return findArticle(1, size, null, null, "createDate", "DESC", "articleAuditstatus=1 and articleHidden=0");
	}
	
	public static List<play.db.Model> recentArticle(int size,long articleCategoryId){
		return findArticle(1, size, null, null, "createDate", "DESC", "articleAuditstatus=1 and articleHidden=0 and articleCategory_id=" + articleCategoryId);
	}
	
	public static List<play.db.Model> findArticle(int page, int size, String search, String searchFields, String orderBy, String order, String where){
		int offset = (page - 1) * size;
        List<String> properties = searchFields == null ? new ArrayList<String>(0) : Arrays.asList(searchFields.split("[ ]"));
        return Model.Manager.factoryFor(Article.class).fetch(offset, size, orderBy, order, properties, search, where);
	}
	

}
